package collector

import (
	"fmt"
	"github.com/gomodule/redigo/redis"
	"github.com/prometheus/common/log"
)

var (
	RedisPool = &redis.Pool{}
)

func initRedisPool() *redis.Pool {
	return &redis.Pool{
		MaxIdle:   10,
		MaxActive: 30,
		Wait:      true,
		Dial: func() (redis.Conn, error) {
			c, err := redis.Dial("tcp", fmt.Sprintf("%s:%s", RancherExporterConf.RedisHost, RancherExporterConf.RedisPort))
			if err != nil {
				log.Error(err)
				return c, err
			}
			if RancherExporterConf.RedisPassword != "" {
				if _, err := c.Do("AUTH", RancherExporterConf.RedisPassword); err != nil {
					log.Error(err)
					c.Close()
					return nil, err
				} else {
					c.Do("SELECT", RancherExporterConf.RedisDB)
				}
			} else {
				c.Do("SELECT", RancherExporterConf.RedisDB)
			}
			return c, err
		},
	}
}
